﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    [RequireComponent(typeof(Toggle))]
    [AddComponentMenu("Nirvana/UI/Bind/Event Bind Toggle")]
    public class UIEventBindToggle : UIEventBind
    {
        [SerializeField]
        [EventName]
        private string eventName;
        private Toggle _toggle;
        private SignalEvent _signalevent;


        protected override void Awake()
        {
            base.Awake();
            this._toggle = base.GetComponent<Toggle>();
        }

        protected override void RefreshBind()
        {
            this._signalevent = base.GetSignalEvent(this.eventName);
        }

        private void OnEnable()
        {
            this._toggle.onValueChanged.AddListener(OnValueChanged);
        }

        private void OnDisable()
        {
            this._toggle.onValueChanged.RemoveListener(OnValueChanged);
        }

        private SignalEvent FindSignalEvent()
        {
            if (this._signalevent == null)
            {
                this._signalevent = base.GetSignalEvent(this.eventName);
            }
            return this._signalevent;
        }
        private void OnValueChanged(bool IsToggle)
        {
            SignalEvent _event = this.FindSignalEvent();
            if (_event != null)
            {
                _event.Excute(IsToggle);
            }
        }

    }
}

